/*******************************************
Module: zt_fs.c
E-Mail: 24zhutian@gmail.com
Notices: Copyright (c) 2007-2011 ls
*******************************************/
#include "zt_config.h"
#include "zt_fs.h"

ZT_API zt_int64_t zt_fs_file_size(const char *lpszFilePath)
{
	struct _stati64 s;
	int fd=-1,n=0;
#ifdef _MSC_VER	
	if((fd=_open(lpszFilePath,_O_RDONLY))==-1){
		return((zt_int64_t)-1);
	}
	n= _fstati64(fd,&s);
	_close(fd);
#endif
	if(n==0){
		return(s.st_size);
	}
	return((zt_int64_t)-1);
}

ZT_API size_t zt_write_file(const char *lpszFilePath,void *p,size_t siz)
{
	FILE *fp=NULL;
	size_t size=0;
	
	fp=fopen(lpszFilePath,"wb");
	if(fp){
		size=fwrite(p,1L,siz,fp);
		fclose(fp);
	}
	return(size);
}

ZT_API size_t zt_read_file(const char *lpszFilePath,void **p,size_t siz)
{
	FILE *fp=NULL;
	size_t size=0;
	long l=0;
	
	fp=fopen(lpszFilePath,"rb");
	if(fp){
		fseek(fp,0L,SEEK_END);
		l=ftell(fp);
		if(((size_t)l)<=siz){
			rewind(fp);
			size=fread(*p,1L,l,fp);
		}
		fclose(fp);
	}
	
	return(size);
}